#include <iostream>
#include <string>
#include <memory>
#include <unistd.h>
#include <pthread.h>
#include <thread>
#include <string.h>
class RwLock
{
private:
    pthread_rwlock_t _rwlock;
public:
    RwLock()
    {
        int n = pthread_rwlock_init(&_rwlock,nullptr);
        if(n != 0)
        {
            std::cout<<"初始化读写锁失败!,错误信息: "<< strerror(errno)<<std::endl;
            abort();
        }
    }
    //读者加锁
    void rlock()
    {
        int n = pthread_rwlock_rdlock(&_rwlock);
        if(n != 0)
        {
            std::cout<<"读者加锁失败!,错误信息: "<< strerror(errno)<<std::endl;
            abort();
        }
    }
    void unlock()
    {
        pthread_rwlock_unlock(&_rwlock);
    }
    void wlock()
    {
        int n = pthread_rwlock_wrlock(&_rwlock);
        if(n != 0)
        {
            std::cout<<"写者加锁失败!,错误信息: "<< strerror(errno)<<std::endl;
            abort();
        }
    }
    ~RwLock()
    {
        pthread_rwlock_destroy(&_rwlock);
    }
};
